<template>
  <!-- 放置一个图标 -->
  <div>
    <svg-icon
      :icon-class="isFullScreen ? 'exit-fullscreen' : 'fullscreen'"
      class="fullscreen"
      @click="toggleScreen"
    />
  </div>
</template>

<script>
export default {
  name: 'ScreenFull',
  data() {
    return {
      isFullScreen: false
    }
  },
  mounted() {
    document.addEventListener('fullscreenchange', e => {
      // 监听到屏幕变化，在回调中判断是否已退出全屏 如果已退出全屏 把本地状态修改为false
      const isFull = document.fullscreenElement
      if (!isFull) {
        this.isScreenFull = false
      }
    })
    document.addEventListener('webkitfullscreenchange', e => {
      console.log('fullscreenchange.....')
      // 监听到屏幕变化，在回调中判断是否已退出全屏
      // 如果已退出全屏 把本地状态修改为false
      const isFull = document.fullscreenElement
      if (!isFull) {
        this.isFullScreen = false
      }
    })
    document.addEventListener('mozfullscreenchange', e => {
      console.log('fullscreenchange.....')
      // 监听到屏幕变化，在回调中判断是否已退出全屏
      // 如果已退出全屏 把本地状态修改为false
      const isFull = document.fullscreenElement
      if (!isFull) {
        this.isFullScreen = false
      }
    })
  },
  methods: {
    toggleScreen() {
      const fn = document.documentElement.webkitRequestFullScreen ||
      document.documentElement.RequestFullScreen ||
      document.documentElement.mozRequestFullScreen

      this.isFullScreen = !this.isFullScreen

      if (this.isFullScreen === true) {
        // window.fn()
        fn.call(document.documentElement)
        // document.documentElement.requestFullscreen()
        document.documentElement.webkitRequestFullScreen()
      } else {
        document.exitFullscreen()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.fullscreen {
  width: 20px;
  height: 20px;
  color: #fff;
  cursor: pointer;
}
</style>
